/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package org.myontology.specialpages;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class PageImpl implements Pageable{

	protected static Logger logger = Logger.getLogger(SpecialPage.class);
	protected List<EntryRow> entries= new ArrayList<EntryRow>();
	private int page = 0;
	private int pageSize=0;

	
	public PageImpl(int pS) {
		pageSize=pS;
	}

	public int getPages() {
		int pageNum=1;
		if (isPageable()) {
			pageNum = getListSize()/getPageSize()+1;
		}
		return pageNum;
	}

	public int getCurrentPageIndex() {
		return page;
	}

	public List<EntryRow> getCurrentPage() {
		List<EntryRow> current=entries;
		if (getPages()>1) {
			int fromIndex = getCurrentPageIndex()*getPageSize();
			int toIndex = fromIndex+getPageSize();
			if (toIndex>getListSize()) {
				toIndex=getListSize();
			}
			current = entries.subList(fromIndex, toIndex);
		}
		return current;
	}

	public void nextPage() {
		page = page==getPages()-1?getPages()-1:++page;
	}

	public void previousPage() {
		page = page==0?0:--page;
	}

	public void firstPage() {
		page=0;
	}

	public void lastPage() {
		page = getPages()-1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public boolean isPageable() {
		return pageSize>0;
	}

	public int getListSize() {
		return entries.size();
	}

}
